package com.genITeam.ria.bl;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.vo.*;
import com.genITeam.ria.dao.*;
import com.genITeam.ria.exception.NGFException;

public class ThreadBL {

	public ThreadBL() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(ThreadBL.class);

	/**
	 * saveThread Save the newly created Thread
	 * 
	 * @param posts
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void saveThread(Posts threadPostVo) throws NGFException {

		ThreadDAO threadDAO = null;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start saveThread");
			threadDAO = new ThreadDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			threadDAO.saveThread(threadPostVo, session);
			tx.commit();
		} catch (Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());
		}

	}

	/**
	 * replyThread Save the Reply Thread
	 * 
	 * @param posts
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void replyThread(Posts threadPostVo) throws NGFException {
		ThreadDAO threadDAO = null;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start replyThread");
			threadDAO = new ThreadDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			threadDAO.replyThread(threadPostVo, session);
			tx.commit();
		} catch (Exception e) {
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	public static void main(String a[]) {

		// ThreadBL.saveThread("2","2","2","2");
	}

}
